var addButtonEventF =function (btn,index,efunc) {
    btn.on(cc.Node.EventType.TOUCH_END, function(event){
        if (efunc) { efunc(index); }
    });

};


var DN_CMD = require("zjhCMD");
cc.Class({
    extends: cc.Component,

    properties: {
        className:"chatlayer",
        content:{
            default:null,
            type:cc.Component,
        },
        chatNode:{
            default:null,
            type:cc.Prefab,
        },
        chatboxleft:{
            default:null,
            type:cc.SpriteFrame,
        },
        chatboxright:{
            default:null,
            type:cc.SpriteFrame,
        },
    },
    onLoad: function () {
        this.node.thisJS = this; 
        //this.content = cc.find("content",this.node);
        this.citem = cc.find("citem",this.node);
        this.chatContent = [
            "玩游戏,请先进群",
            "群内游戏,切勿转发",
            "别磨蹭,快点打牌",
            "我出去叫人",
            "你的牌好靓哇",
            "我当年横扫澳门五条街",
        ]
        this.textPanel = cc.find("textpanel",this.node);
        this.bgpanel = cc.find("bgpanel",this.node);
        this.bgmask = cc.find("bgmask",this.node);
        EventManager.addEventObeserveF(EventConfig.EvtDeskChat, handler(this,
            this.showText),"chatlayer"); 
        EventManager.addEventObeserveF(EventConfig.chatClick, handler(this,
            this.chatClick),"chatlayer");     
        this.updateView();
        // this.schedule(function(){
        //     this.test();
        // },4);
        
    },

    test:function(){
         for(i=0;i<12;i++){
            var posConfig = GameConfig.chatConfigEle[i];
            var chatNode = cc.instantiate(this.chatNode);
            this.textPanel.addChild(chatNode);
           
            var content = cc.find("content",chatNode);
            content.getComponent(cc.Label).string = "我当年横扫澳门五条街";
            var size = content.getContentSize();
            dump(size)
            chatNode.setContentSize({width:size.width+50,height:75});
            if(posConfig.type==1){
                content.setPosition(cc.p(40,45));
                chatNode.setPosition(posConfig.pos);
                chatNode.setAnchorPoint(posConfig.anchor);
                chatNode.getComponent(cc.Sprite).spriteFrame = this.chatboxleft ;
            }else{
                content.setPosition(cc.p(10,45));
                chatNode.setPosition(cc.p(posConfig.pos.x-size.width-50,posConfig.pos.y));
                chatNode.setAnchorPoint(posConfig.anchor);
                chatNode.getComponent(cc.Sprite).spriteFrame = this.chatboxright ;
            }
            
            var delay     = cc.delayTime(1);
            var fadeout      = cc.fadeOut(1);
            var call  = cc.callFunc(function (chatNode){
                chatNode.removeFromParent(true);
            },this,chatNode)
            var sq = cc.sequence(delay,fadeout,call);
            chatNode.runAction(sq); 
        }
    },

    updateView:function(){
        var self = this; 
        for(var i=0;i<this.chatContent.length;i++){
            var tempItem = cc.instantiate(this.citem);
            var content = cc.find("content",tempItem);
            this.content.node.addChild(tempItem);
            content.getComponent(cc.Label).string = this.chatContent[i];
            addButtonEventF(tempItem,i,function(index){
                self.chatReq(self.chatContent[index]);
            });
        }        
    },

    chatClick:function(){
        console.log("chatClick");
        if (this.bgpanel.active){
            this.bgpanel.active = false ;
            this.bgmask.active = true ;
        }else{
            this.bgpanel.active = true ;
            this.bgmask.active = true ;
            console.log("chatClick show");
        }
    },
    /*
    // 台桌内聊天通知
    message EvtDeskChat {
        optional int32 op_uin = 1;
        optional string nick  =2;
        optional string content =3;
        optional int32 uin =4;
        optional int32 deskid=5;
        optional int32 roomid=6;
        optional DeskPlayInfo play_info =7;
        optional int32 gender = 8;
        optional string portrait = 9; // 头像url
    }
    */
    chatReq:function(content){
        this.bgpanel.active = false ;
        this.bgmask.active = false ;
        console.log("发送文字消息");
        var temp =  {cmd:DN_CMD.DeskChatReq,
            body:{
                content:content,
            }}
        dump(temp);
        GameNet.sendRequest(temp);
    },

    showText:function(data){
        console.log("showText");
        var info = GameData ;
        for(var k in info.playerinfo){
            if(info.playerinfo[k].uin==data.op_uin){
                console.log("显示消息");
                var user = info.playerinfo[k];
                var searId = GameData.getUserSeatP(user.seatid);
                var posConfig = GameConfig.chatConfig[searId];
                var chatNode = cc.instantiate(this.chatNode);
                this.textPanel.addChild(chatNode);
               
                var content = cc.find("content",chatNode);
                content.getComponent(cc.Label).string = data.content;
                var size = content.getContentSize();
                dump(size)
                chatNode.setContentSize({width:size.width+50,height:75});
                if(posConfig.type==1){
                    content.setPosition(cc.p(40,45));
                    chatNode.setPosition(posConfig.pos);
                    chatNode.setAnchorPoint(posConfig.anchor);
                    chatNode.getComponent(cc.Sprite).spriteFrame = this.chatboxleft ;
                }else{
                    content.setPosition(cc.p(10,45));
                    chatNode.setPosition(cc.p(posConfig.pos.x-size.width-50,posConfig.pos.y));
                    chatNode.setAnchorPoint(posConfig.anchor);
                    chatNode.getComponent(cc.Sprite).spriteFrame = this.chatboxright ;
                }
                
                var delay     = cc.delayTime(1);
                var fadeout      = cc.fadeOut(1);
                var call  = cc.callFunc(function (chatNode){
                    chatNode.removeFromParent(true);
                },this,chatNode)
                var sq = cc.sequence(delay,fadeout,call);
                chatNode.runAction(sq); 
            }
        }
    },

    exit:function(){
        this.bgpanel.active = false ;
        this.bgmask.active = false ;
    },

});